﻿<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="<%=basePath%>plugins/h_uiadmin_v2.3.4/css/H-ui.min.css" rel="stylesheet" type="text/css" />
<link href="<%=basePath%>plugins/h_uiadmin_v2.3.4/css/H-ui.admin.css" rel="stylesheet" type="text/css" />
<link href="<%=basePath%>plugins/h_uiadmin_v2.3.4/lib/icheck/icheck.css" rel="stylesheet" type="text/css" />
<link href="<%=basePath%>plugins/h_uiadmin_v2.3.4/lib/Hui-iconfont/1.0.7/iconfont.css" rel="stylesheet" type="text/css" />

<title>新增字典值</title>
</head>
<body>
 <div class="pd-20">
  <form  method="post" class="form form-horizontal" id="form1">
  
    <div class="row cl">
      <label class="form-label col-3"><span class="c-red">*</span>实体描述：</label>
      <div class="formControls col-5">
           <select class="select"  size="1" id="tableName" name="tableName" style="width:100px" datatype="*" nullmsg="实体描述不能为空">
		      <option value="" selected="selected">请选择</option>
		      <c:if test="${tableList !=null}">
		       <c:forEach items="${tableList}" var="tl">
		        <option value="${tl.code}"> ${tl.description}</option>
		       </c:forEach>
		      </c:if>
		   </select>
      </div>
      <div class="col-4"> </div>
    </div>
    <input type="hidden" id="tableDesc" name="tableDesc" value="">
    <div class="row cl">
      <label class="form-label col-3"><span class="c-red">*</span>属性描述：</label>
      <div class="formControls col-5">
         <select class="select"  size="1" id="attributeName" name="attributeName" style="width:150px" datatype="*" nullmsg="属性描述不能为空">
		      <option value="" selected="selected">请选择</option>
		      
		 </select>
      </div>
      <div class="col-4"> </div>
    </div>
     <input type="hidden" id="attributeDesc" name="attributeDesc" value="">
    
    <div class="row cl">
      <label class="form-label col-3">属性值：</label>
      <div class="formControls col-5">
          <input type="text" class="input-text" placeholder="请填写数字" name="value" id="value" value="" datatype="*" nullmsg="属性值不能为空">
       </div>
      <div class="col-4"> </div>
    </div>
    
    <div class="row cl">
      <label class="form-label col-3"><span class="c-red"></span>状态：</label>
      <div class="formControls col-5">
          <select class="select"  size="1" id="status" name="status" style="width:100px">
		      <option value="1" selected="selected">可用</option>
		      <option value="2">不可用</option>
		   </select>
      </div>
      <div class="col-4"> </div>
    </div>
    
     <div class="row cl">
      <label class="form-label col-3">描述：</label>
      <div class="formControls col-5">
          <input type="text" class="input-text" placeholder="" name="description" id=""description"" value="" datatype="*" nullmsg="描述不能为空">
       </div>
      <div class="col-4"> </div>
    </div>
    
    <div class="row cl">
      <label class="form-label col-3">编码：</label>
      <div class="formControls col-5">
          <input type="text" class="input-text" placeholder="" name="code" id="code" value="" >
       </div>
      <div class="col-4"> </div>
    </div>
    
    <div class="row cl">
      <div class="col-9 col-offset-3">
        <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
        &nbsp;&nbsp;<input id="closeBtn"  class="btn btn-primary radius" type="button" value="&nbsp;&nbsp;关闭&nbsp;&nbsp;">
      </div>
    </div>
  </form>
</div>
</div>
<script type="text/javascript" src="<%=basePath%>plugins/h_uiadmin_v2.3.4/lib/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript" src="<%=basePath%>plugins/h_uiadmin_v2.3.4/lib/icheck/jquery.icheck.min.js"></script> 
<script type="text/javascript" src="<%=basePath%>plugins/h_uiadmin_v2.3.4/lib/Validform/5.3.2/Validform.min.js"></script>
<script type="text/javascript" src="<%=basePath%>plugins/h_uiadmin_v2.3.4/lib/layer/2.1/layer.js"></script>
<script type="text/javascript" src="<%=basePath%>plugins/h_uiadmin_v2.3.4/lib/My97DatePicker/WdatePicker.js"></script> 
<script type="text/javascript" src="<%=basePath%>plugins/h_uiadmin_v2.3.4/js/H-ui.js"></script> 
<script type="text/javascript" src="<%=basePath%>plugins/h_uiadmin_v2.3.4/js/H-ui.admin.js"></script>
<script type="text/javascript" src="<%=basePath%>common/js/cms.js"></script>
<script type="text/javascript">
$(function(){
	 $('#attributeName').attr('disabled','disabled');
	
	// 从父页面初始化值
	initVal();
	 
	$('.skin-minimal input').iCheck({
		checkboxClass: 'icheckbox-blue',
		radioClass: 'iradio-blue',
		increaseArea: '20%'
	});
	
	$("#form1").Validform({
		tiptype:2,
		beforeSubmit:function(curform){
			 //在验证成功后，表单提交前执行的函数，curform参数是当前表单对象。
            //这里明确return false的话表单将不会提交;
			submitForm(curform) ;
			return false ;
		},
		callback:function(form){
			
			// submitForm(form) ;
			// form[0].submit();
			return false ;
		}
	});
	
	function submitForm(form ){
		var params = JSON.stringify( $("#form1").serializeJson()) ;
		$.ajax({
			"type": "POST", 
			"url": "<%=basePath%>dictionarymgr/doadd"  , 
			"dataType": "json",  // 预期服务器返回的数据类型
			"data": "params="+params ,      
			"success": function(resp) {
				// console.log(resp) ;
				if(resp.success){
					    var index = parent.layer.getFrameIndex(window.name);
						//parent.$('.btn-refresh').click();
						parent.$('#queryBtn').click(); 
						parent.layer.close(index);
						
				}else{
					// alert(resp.errMsg+",请重试!") ;
					   layer.alert( resp.errMsg+",请重试!", {icon: 2},function(index){
						 layer.close(index);
					  });
				}
				 
			}
		}); 
	}
	
	
	$("#closeBtn").click(function(){
		var index = parent.layer.getFrameIndex(window.name);
		parent.layer.close(index);
	});
	
	 /*
	 * attributeName添加change事件
	 */
	 $('#attributeName').change(function(){
		 $('#attributeDesc').val( $('#attributeName').find("option:selected").text() )  ;
	 }) ;
	 /*
	 *  tableName添加change事件
	 */
	 $('#tableName').change(function(){
		 getAttributeNameValFromServer();
	 
	});

   function getAttributeNameValFromServer(){
	   $('#tableDesc').val( $('#tableName').find("option:selected").text() )  ; // 设值
		  
		 $('#attributeName').removeAttr('disabled');
		 $('#attributeName').empty();
		 var html = '<option value="" selected="selected">请选择</option>' ;
		 var tableVal = $('#tableName').val()  ;
		 $.ajax({
				url:"<%=basePath%>dictionarymgr/getattrname" ,
				type:'POST',
				data:{'code':tableVal},
				async:false,  // 先改成同步调用
				dataType:'json',
				success:function(resp){
					// console.log(resp) ;
					 $('#attributeName').html(html);
					 if(resp.success){
							   var data = resp.data ;
							  if(data == null || data == '' || data==undefined){
								 /*  layer.alert("还没有配值", {icon: 2},function(index){
										 layer.close(index);
									  }); */
								  return ;
							  }
							  $.each(data,function(i,item){
									html+='<option value="'+item.code+'">'+item.description+'</option>';
								});
								$('#attributeName').html(html);  
						}
					 /* else{
							      layer.alert(resp.errMsg, {icon: 2},function(index){
								 layer.close(index);
							  });  
						} */
				},
				error:function(err){
					   layer.alert( err, {icon: 2},function(index){
							 layer.close(index);
						  });  
				}
			 
	   });
	 }
   
   function initVal(){
	     var p_tableName = parent.$("#tableName").val();
		 var p_attributeName = parent.$("#attributeName").val();
		 
		// 从父页面带值
		if(p_tableName.length > 0){
			var count = $("#tableName option").length;
			 for(var i=0;i<count;i++){
				 if($("#tableName").get(0).options[i].value == p_tableName) {
					 $("#tableName").get(0).options[i].selected = true;        
					 getAttributeNameValFromServer();  // 同步调用，下面方法才能赋值成功，否则是异步调用，赋值不成功
					 break ;
				 }
			 }
		}
	 
		if(p_attributeName.length > 0){
			var count = $("#attributeName option").length;
			for(var i=0;i<count;i++){
				 if($("#attributeName").get(0).options[i].value == p_attributeName) {
					 $("#attributeName").get(0).options[i].selected = true;        
					 $('#attributeDesc').val( $('#attributeName').find("option:selected").text() )  ;
					 break ;
				 }
			 }
		}
	    
   }
	 
});
</script>
</body>
</html>